import 'package:flutter/material.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/models/report/evaluationLevelChartData.dart';
import 'package:ichinesereader/utils/helper.dart';
import 'package:ichinesereader/utils/log.dart';
import 'package:syncfusion_flutter_charts/charts.dart';
import 'package:get/get.dart';

class EvaluationLevelStackedLines extends StatelessWidget {
  EvaluationLevelStackedLines(
      {Key? key,
      required this.levelChartData,
      required this.chartTitle,
      required this.xLabel,
      this.levelStandard = 'ICR',
      required this.yLabel,
      this.mode = 'evaluation'})
      : super(key: key);
  final List<EvaluationLevelChart> levelChartData;
  final String chartTitle;
  final String xLabel;
  final String yLabel;
  final String? levelStandard;
  final String? mode;
  @override
  Widget build(BuildContext context) {
    Log.debug('build evaluation Level stacked lines Widget');
    TooltipBehavior _tooltipbehavior = TooltipBehavior(
        enable: true,
        duration: 1800,
        //format: 'Level: ${point.x} \n Score: point.y',
        builder: (dynamic data, dynamic point, dynamic series, int pointIndex,
            int seriesIndex) {
          return Padding(
            padding: const EdgeInsets.all(2.0),
            child: Container(
                color: Colors.grey[100],
                child: Text(
                    '${series.name}\nLevel: ${point.x.toString()} \nScore: ${point.y.toString()}')),
          );
        });

    List<CategoricalMultiLevelLabel> _xAxisCategories = Constants
        .levelAxisCategories
        .where((x) => x.start != '18' && x.start != '20')
        .toList();

    return SfCartesianChart(
        title: ChartTitle(
          text: this.chartTitle,
          textStyle: TextStyle(color: Theme.of(context).primaryColorDark),
        ),
        legend: Legend(
            isVisible: true,
            position: LegendPosition.top,
            overflowMode: LegendItemOverflowMode.scroll),
        tooltipBehavior: _tooltipbehavior,
        primaryXAxis: CategoryAxis(
            plotBands: levelStandard == 'ICR'
                ? Constants.plotbandIChineseReader
                    .where((element) => element.start < 16)
                    .toList()
                : Constants.plotbandACTFL
                    .where((element) => element.start < 5)
                    .toList(),
            multiLevelLabels: _xAxisCategories),
        plotAreaBackgroundColor: Colors.grey[100],
        primaryYAxis: NumericAxis(
            title: AxisTitle(text: this.xLabel),
            minimum: 0,
            maximum: 10,
            interval: 1),
        series: _buildSeries(this.mode));
  }

  dynamic _buildSeries(String? mode) {
    if (mode == "quickView") {
      return [
        StackedLineSeries<EvaluationLevelChart, String>(
          groupName: 'speaking',
          name: 'Speaking'.tr,
          dataSource: levelChartData,
          xValueMapper: (EvaluationLevelChart data, _) =>
              data.level.replaceAll('ACTFLLevel.', ''),
          yValueMapper: (EvaluationLevelChart data, _) => data.speaking,
          color: hexStringToHexInt('#f8ac59'),
          dataLabelSettings: DataLabelSettings(isVisible: false),
        ),
        StackedLineSeries<EvaluationLevelChart, String>(
          groupName: 'writing',
          name: 'Writing'.tr,
          dataSource: levelChartData,
          xValueMapper: (EvaluationLevelChart data, _) =>
              data.level.replaceAll('ACTFLLevel.', ''),
          yValueMapper: (EvaluationLevelChart data, _) => data.writing,
          color: hexStringToHexInt('#1ab394'),
          dataLabelSettings: DataLabelSettings(isVisible: false),
        ),
        StackedLineSeries<EvaluationLevelChart, String>(
          groupName: 'aiSpeaking',
          name: 'AI Speaking'.tr,
          dataSource: levelChartData,
          xValueMapper: (EvaluationLevelChart data, _) =>
              data.level.replaceAll('ACTFLLevel.', ''),
          yValueMapper: (EvaluationLevelChart data, _) => data.aiSpeaking,
          color: hexStringToHexInt('#9d98f4'),
          dataLabelSettings: DataLabelSettings(isVisible: false),
        ),
        StackedLineSeries<EvaluationLevelChart, String>(
          groupName: 'translate',
          name: 'Translating'.tr,
          dataSource: levelChartData,
          xValueMapper: (EvaluationLevelChart data, _) =>
              data.level.replaceAll('ACTFLLevel.', ''),
          yValueMapper: (EvaluationLevelChart data, _) => data.translate,
          color: hexStringToHexInt('#9d98f4'),
          dataLabelSettings: DataLabelSettings(isVisible: false),
        ),
      ];
    } else if (mode == "benchmark") {
      return [
        StackedLineSeries<EvaluationLevelChart, String>(
          groupName: 'listening',
          name: 'Listening'.tr,
          dataSource: levelChartData,
          xValueMapper: (EvaluationLevelChart data, _) =>
              data.level.replaceAll('ACTFLLevel.', ''),
          yValueMapper: (EvaluationLevelChart data, _) => data.listening,
          color: hexStringToHexInt('#ed5565'),
          dataLabelSettings: DataLabelSettings(isVisible: false),
        ),
        StackedLineSeries<EvaluationLevelChart, String>(
          groupName: 'reading',
          name: 'Reading'.tr,
          dataSource: levelChartData,
          xValueMapper: (EvaluationLevelChart data, _) =>
              data.level.replaceAll('ACTFLLevel.', ''),
          yValueMapper: (EvaluationLevelChart data, _) => data.reading,
          color: hexStringToHexInt('#23c6c8'),
          dataLabelSettings: DataLabelSettings(isVisible: false),
        ),
        StackedLineSeries<EvaluationLevelChart, String>(
          groupName: 'speaking',
          name: 'Speaking'.tr,
          dataSource: levelChartData,
          xValueMapper: (EvaluationLevelChart data, _) =>
              data.level.replaceAll('ACTFLLevel.', ''),
          yValueMapper: (EvaluationLevelChart data, _) => data.speaking,
          color: hexStringToHexInt('#f8ac59'),
          dataLabelSettings: DataLabelSettings(isVisible: false),
        ),
        StackedLineSeries<EvaluationLevelChart, String>(
          groupName: 'writing',
          name: 'Writing'.tr,
          dataSource: levelChartData,
          xValueMapper: (EvaluationLevelChart data, _) =>
              data.level.replaceAll('ACTFLLevel.', ''),
          yValueMapper: (EvaluationLevelChart data, _) => data.writing,
          color: hexStringToHexInt('#1ab394'),
          dataLabelSettings: DataLabelSettings(isVisible: false),
        ),
      ];
    } else {
      return [
        StackedLineSeries<EvaluationLevelChart, String>(
          groupName: 'listening',
          name: 'Listening'.tr,
          dataSource: levelChartData,
          xValueMapper: (EvaluationLevelChart data, _) =>
              data.level.replaceAll('ACTFLLevel.', ''),
          yValueMapper: (EvaluationLevelChart data, _) => data.listening,
          color: hexStringToHexInt('#ed5565'),
          dataLabelSettings: DataLabelSettings(isVisible: false),
        ),
        StackedLineSeries<EvaluationLevelChart, String>(
          groupName: 'reading',
          name: 'Reading'.tr,
          dataSource: levelChartData,
          xValueMapper: (EvaluationLevelChart data, _) =>
              data.level.replaceAll('ACTFLLevel.', ''),
          yValueMapper: (EvaluationLevelChart data, _) => data.reading,
          color: hexStringToHexInt('#23c6c8'),
          dataLabelSettings: DataLabelSettings(isVisible: false),
        ),
        StackedLineSeries<EvaluationLevelChart, String>(
          groupName: 'speaking',
          name: 'Speaking'.tr,
          dataSource: levelChartData,
          xValueMapper: (EvaluationLevelChart data, _) =>
              data.level.replaceAll('ACTFLLevel.', ''),
          yValueMapper: (EvaluationLevelChart data, _) => data.speaking,
          color: hexStringToHexInt('#f8ac59'),
          dataLabelSettings: DataLabelSettings(isVisible: false),
        ),
        StackedLineSeries<EvaluationLevelChart, String>(
          groupName: 'writing',
          name: 'Writing'.tr,
          dataSource: levelChartData,
          xValueMapper: (EvaluationLevelChart data, _) =>
              data.level.replaceAll('ACTFLLevel.', ''),
          yValueMapper: (EvaluationLevelChart data, _) => data.writing,
          color: hexStringToHexInt('#1ab394'),
          dataLabelSettings: DataLabelSettings(isVisible: false),
        ),
        StackedLineSeries<EvaluationLevelChart, String>(
          groupName: 'aiSpeaking',
          name: 'AI Speaking'.tr,
          dataSource: levelChartData,
          xValueMapper: (EvaluationLevelChart data, _) =>
              data.level.replaceAll('ACTFLLevel.', ''),
          yValueMapper: (EvaluationLevelChart data, _) => data.aiSpeaking,
          color: hexStringToHexInt('#9d98f4'),
          dataLabelSettings: DataLabelSettings(isVisible: false),
        ),
      ];
    }
  }
}
